//
//  Block.m
//  CarDemo
//
//  Created by huang kun on 8/9/10.
//  Copyright 2010 UESTC. All rights reserved.
//

#import "CurrentBlock.h"


@implementation CurrentBlock

@synthesize delegate;
@synthesize type;
@synthesize blockNum;
@synthesize avaliable;

- (void) setType:(int)t {
	switch (t) {
		case kblockType1:
		{
			type = t;
			[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"bigBlock1.png"]];
		}
			break;
		case kblockType2:
		{
			type = t;
			[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"bigBlock2.png"]];
		}
			break;
		case kblockType3:
		{
			type = t;
			[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"bigBlock3.png"]];
		}
			break;
		case kblockType4:
		{
			type = t;
			[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"bigBlock4.png"]];
		}
			break;
		case kblockType5:
		{
			type = t;
			[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"bigBlock5.png"]];
		}
			break;
		default:
		{
			type = kblockTypeDefault;
			[self setTextureRect:CGRectZero];
			avaliable = NO;
		}
			break;
	}
	[self setTextureRect:CGRectMake(0, 0, 48, 48)];

}


- (void) dealloc {
	delegate = nil;
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}

- (void) onEnter{
	[super onEnter];
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

- (void) onExit{
	[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
	[super onExit];
}


- (id) init{
	//读取文件数据
	if( (self = [super init]) ){		
		delegate = nil;
		avaliable = YES;
		roadBlock = nil;
		shouldPlant = NO;
		draging = NO;
		dragingPosition = CGPointZero;
		
		
		[self setType:randomBlockType];
		nextBlock = [NextBlock node];
		[nextBlock setType:randomBlockType];
		[self addChild:nextBlock];
		[nextBlock setPosition:ccp(self.contentSize.width/2, -60+self.contentSize.height/2)];
		
		usedBlock = 2;
	}
	return self;
}

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
	CGPoint p = [touch locationInView:[touch view]];
	p = [[CCDirector sharedDirector] convertToGL:p];
	shouldPlant = NO;
	
	if(avaliable && CGRectContainsPoint(self.boundingBox, p)){
		roadBlock = [Block blockWithType:type];//[CCSprite spriteWithFile:[NSString stringWithFormat:@"smallBlock%d.png",type]];
		[self addChild:roadBlock];
		p = [self convertToNodeSpace:p];
		[roadBlock setPosition:p];
		
		draging = YES;
		return YES;
	}
	else {
		return NO;
	}
}

- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
	CGPoint p = [touch locationInView:[touch view]];
	p = [[CCDirector sharedDirector] convertToGL:p];
	dragingPosition = [delegate convertToGridPosition:p];
	if ([delegate shouldPlantAtPosition:dragingPosition])
		shouldPlant = YES;
	else 
		shouldPlant = NO;

	if( !CGPointEqualToPoint(dragingPosition, CGPointZero)){
		p = [self convertToNodeSpace:dragingPosition];	
		[roadBlock setPosition:p];
	}
	else {
		p = [self convertToNodeSpace:p];	
		[roadBlock setPosition:p];
	}
}


- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
	CGPoint p = [touch locationInView:[touch view]];
	p = [[CCDirector sharedDirector] convertToGL:p];
	
	draging = NO;
	if(delegate){
		[roadBlock retain];
		[self removeChild:roadBlock cleanup:YES];
		if( [delegate addBlock:roadBlock position:p] )
			[self update];
		[roadBlock release];
		roadBlock = nil;
	}
	else {
		[self removeChild:roadBlock cleanup:YES];
		roadBlock = nil;
	}
}

- (void) update {
	usedBlock++;
	if(usedBlock > blockNum)
		[nextBlock setType:kblockTypeDefault];
	[self setType:nextBlock.type];
	[nextBlock setType:randomBlockType];
}
			
- (void) draw{
	[super draw];
	if(draging && CGRectContainsPoint(roadRect, dragingPosition)){
		
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		glDisable(GL_TEXTURE_2D);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glEnable(GL_BLEND);
		
		if(shouldPlant){
			//可以种植
			const GLubyte squareColours[] = {
				255,255,255,150,
				255,255,255,150,
				255,255,255,150,
				255,255,255,150
			};
			glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColours);
		}
		else {
			//不能种植
			const GLubyte squareColours[] = {
				0,0,0,150,
				0,0,0,150,
				0,0,0,150,
				0,0,0,150
			};
			glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColours);
		}
		
		CGRect rect = roadRect;
		CGPoint vertices1[4] = {
			[self convertToNodeSpace: ccp(rect.origin.x, dragingPosition.y - 0.5*roadHeight)],
			[self convertToNodeSpace: ccp(rect.origin.x + rect.size.width, dragingPosition.y - 0.5*roadHeight)],
			[self convertToNodeSpace: ccp(rect.origin.x + rect.size.width, dragingPosition.y + 0.5*roadHeight)],
			[self convertToNodeSpace: ccp(rect.origin.x, dragingPosition.y + 0.5*roadHeight)]
		};
		CGPoint vertices2[4] = {
			[self convertToNodeSpace: ccp(dragingPosition.x - 0.5*roadWidth, rect.origin.y)],
			[self convertToNodeSpace: ccp(dragingPosition.x + 0.5*roadWidth, rect.origin.y)],
			[self convertToNodeSpace: ccp(dragingPosition.x + 0.5*roadWidth, rect.origin.y + rect.size.height)],
			[self convertToNodeSpace: ccp(dragingPosition.x - 0.5*roadWidth, rect.origin.y + rect.size.height)]
		};
		
		glVertexPointer(2, GL_FLOAT, 0, vertices1);
		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		
		glVertexPointer(2, GL_FLOAT, 0, vertices2);
		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		
		
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glEnable(GL_TEXTURE_2D);
	}
}




@end
